import pandas as pd

from src.Object import Object


class Frame(Object):
    SIZE = 25

    def __init__(self):
        self.current_time = int
        self.TAG = '[Frame]'
        self.header = None
        self.contents = []
        self.summary = None
        self.content_size = 0
        self.data_frame = pd.DataFrame()
        self.tail_series = pd.Series()

    def add_head(self, elements):
        elements.append('time')
        self.header = elements

    def add_tail(self, elements):
        elements = Frame.format_line(elements)
        data = [elements[3], elements[5], elements[7]]
        self.tail_series = pd.Series(data, index=['free', 'buffers', 'cached'], name=self.current_time)

    def add_content(self, elements):
        elements = Frame.format_line(elements)
        elements.append(self.current_time)

        ser = pd.Series(elements, index=self.header, name=self.current_time)
        self.data_frame = self.data_frame.append(ser)
        self.content_size += 1

    def add_time(self, elements):
        elements = Frame.format_line(elements)
        elements.append(self.current_time)
        self.current_time = elements[1]

    def is_content_full(self):
        return self.content_size >= Frame.SIZE

    def to_string(self):
        print(self.data_frame)

    @staticmethod
    def format_line(elements):
        new_elements = []

        for e in elements:
            if e[-1:] == 'K':
                e = e[:-1]
            new_elements.append(e)

        return new_elements
